<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>老师管理</title>
</head>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="../css/student.css">
<link rel="stylesheet" href="../css/header.css">

<body style="background: url('../img/bg-min.jpg');background-size: cover;">
    <div id="app5">
        <div class="header">
            <div class="logo">
                <h1>学生课程管理系统</h1>
            </div>
            <el-menu class="el-menu-nav" mode="horizontal" background-color="#545c64" text-color="#fff"
                active-text-color="#ffd04b">
                <el-submenu index="1">
                    <template slot="title">个人信息</template>
                    <el-menu-item @click="toMember">{{userName}}</el-menu-item>
                </el-submenu>
            </el-menu>
            <div class="line"></div>
        </div>
        <div class="main">
            <div class="leftMenu" style="width: 145px;">
                <el-row class="tac" style="width: 145px;">
                    <el-menu default-active="2" class="el-menu-vertical-demo" background-color="#545c64"
                        text-color="#fff" active-text-color="#ffd04b">
                        <el-menu-item index="1" @click="toStudent">
                            <span slot="title">学生管理</span>
                        </el-menu-item>
                        <el-menu-item index="2" v-show="adminFlag" @click="toTeacher">
                            <span slot="title">老师管理</span>
                        </el-menu-item>
                        <el-menu-item index="3" @click="toSubject">
                            <span slot="title">课程管理</span>
                        </el-menu-item>
                        <el-menu-item index="4" @click="toTeacherInfo">
                            <span slot="title">个人信息</span>
                        </el-menu-item>
                    </el-menu>
                </el-row>
            </div>
            <div class="rightMenu subject-state">
                <div>
                    <el-button type="primary" class="addStudentBtn" @click="addTeacher">新增</el-button>
                </div>
                <el-table :data="tableData" style="width: 100%">
                    <el-table-column prop="name" label="姓名">
                    </el-table-column>
                    <el-table-column prop="sex" label="性别">
                    </el-table-column>
                    <el-table-column prop="mobile" label="电话">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="updateTeacher(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="deleteTeacher(scope.$index, scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!--新增老师弹窗-->
            <el-dialog title="添加老师信息" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="姓名" :label-width="formLabelWidth">
                        <el-input v-model="form.name" placeholder="姓名" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" :label-width="formLabelWidth">
                        <el-select v-model="form.sex" placeholder="性别">
                            <el-option label="男" value="1"></el-option>
                            <el-option label="女" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="电话" :label-width="formLabelWidth">
                        <el-input v-model="form.mobile" placeholder="电话" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirAdd">确 定</el-button>
                </div>
            </el-dialog>
            <!--编辑老师弹窗-->
            <el-dialog title="编辑老师信息" :visible.sync="dialogFormEditVisible">
                <el-form :model="formEdit">
                    <el-form-item label="姓名" :label-width="formLabelWidth">
                        <el-input v-model="formEdit.name" placeholder="姓名" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" :label-width="formLabelWidth">
                        <el-select v-model="formEdit.sex" placeholder="性别">
                            <el-option label="男" value="1"></el-option>
                            <el-option label="女" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="电话" :label-width="formLabelWidth">
                        <el-input v-model="formEdit.mobile" placeholder="电话" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormEditVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirEdit">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>

</body>
<script>
    var app5 = new Vue({
        el: '#app5',
        data: {
            userName: "",
            url: 'http://127.0.0.1:8080',
            adminFlag: false,
            dialogFormVisible: false,
            dialogFormEditVisible: false,
            tableData: [],
            form: {
                name: '',
                sex: '',
                mobile: '',
            },
            formEdit: {
                name: '',
                sex: '',
                mobile: '',
            },
            formLabelWidth: '120px'
        },
        mounted: function () {
            if (window.localStorage.getItem("userName") == "admin") {
                this.adminFlag = true;
            }
            this.getTeacherList()
            this.userName = window.localStorage.getItem("userName")
        },
        methods: {
            //学生管理
            toStudent: function () {
                window.location.href = "studentManage.html"
            },
            toTeacher: function () { window.location.href = "teacherManage.html" },
            toSubject: function () { window.location.href = "subjectManage.html" },
            toMember: function () {
                window.location.href = "teacherInfo.html"
            },
            toTeacherInfo: function () { window.location.href = "teacherInfo.html" },
            //查询老师列表
            getTeacherList: function () {
                var _this = this;
                axios.get(this.url + '/teacher/query', {
                    params: {
                    }
                }).then(function (res) {
                    if (res.data.msg == 'success') {
                        res = res.data;
                        _this.tableData = res.obj.map((item, v) => {
                            if (item.sex == 1) {
                                item.sex = "男"
                            } else {
                                item.sex = "女"
                            }
                            return item;
                        });
                    }
                })
            },
            //打开新增窗口
            addTeacher: function () {
                this.dialogFormVisible = true
            },
            //确认新增
            confirAdd: function () {
                var _this = this;
                this.judgeRule("add");
                axios.post(this.url + '/teacher/insert', this.form).then(function (res) {
                    if (res.data.msg == 'success') {
                        _this.form = {};
                        _this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        _this.getTeacherList()
                    } else {
                        _this.$message.error('添加失败');
                    }

                });
                this.dialogFormVisible = false;
            },
            //确认编辑
            confirEdit: function () {
                var _this = this;
                this.judgeRule("edit");
                axios.post(this.url + '/teacher/update', this.formEdit).then(function (res) {
                    if (res.data.msg == 'success') {
                        _this.$message({
                            message: '编辑成功',
                            type: 'success'
                        });
                        _this.getTeacherList();
                    } else {
                        _this.$message.error('编辑失败');
                    }
                });
                this.dialogFormEditVisible = false;
            },
            updateTeacher: function (index, row) {
                this.formEdit = row;
                this.dialogFormEditVisible = true
            },
            //删除
            deleteTeacher: function (index, row) {
                var _this = this;
                this.$confirm('是否确定删除此信息', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(this.url + '/teacher/delete?id=' + row.id).then(function (res) {
                        if (res.data.msg == 'success') {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getTeacherList()
                        } else {
                            _this.$message.error('删除失败');
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //字段校验
            judgeRule: function (item) {
                let currentItem = {}
                if (item == "add") {
                    currentItem = this.form
                } else {
                    currentItem = this.formEdit
                }
                if (!currentItem.name) return this.$message({
                    message: '请输入姓名',
                    type: 'warning'
                });
                if (!currentItem.sex) return this.$message({
                    message: '请输入性别',
                    type: 'warning'
                });
                if (!currentItem.mobile) return this.$message({
                    message: '请输入电话号码',
                    type: 'warning'
                })
            }
        }
    });
</script>

</html>
